package com.ms.game.invitefriend.async;

import java.util.ArrayList;

import com.ms.game.invitefriend.db.DistributorInviteCodeDAO;
import com.ms.game.invitefriend.db.DistributorInviteCodesModel;
import com.ms.game.invitefriend.db.DistributorsModel;
import com.ms.game.invitefriend.db.SubDistributorsModel;
import com.ms.game.invitefriend.message.InviteCodeData;
import com.ms.game.invitefriend.message.SGetInviteCodeResponse;
import com.ms.player.Player;
import com.ms.player.PlayerIdentityType;
import com.ms.service.error.ErrorCode;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.task.TaskAsyncCommand;

public class GetDistributorInviteCodeAsync extends TaskAsyncCommand {
	
	/**
	 * 角色
	 */
	public Player player;
	
	/**
	 * sessionID
	 */
	public GUID sessionId;
	
	/**
	 *  MessageTypes.S_INVITE_CODE_GET_DISTRIBUTOR_INVITE_CODE
	 */
	public int messageType;
	
	public int limitCount;
	
	@Override
	public void asyncExecute() throws Exception {
		SGetInviteCodeResponse response = new SGetInviteCodeResponse();
		
		byte identifyType = player.getPlayerIdentityType();
		String playerId = player.getGuid().toString();

		identifyType = player.getPlayerIdentityType();
		
		DistributorsModel distributorsModel = null;
		SubDistributorsModel subDistributorsModel = null;
		if(identifyType== PlayerIdentityType.DISTRIBUTOR || identifyType == PlayerIdentityType.BOTH_DISTRIBUTOR_DOWNLINE) { 
			//經銷商檢查是否是經銷商&是否開啟設定
			distributorsModel = DistributorInviteCodeDAO.getDistributorModelByGUID(playerId);
			if(distributorsModel == null ||!distributorsModel.isEffective()){
				identifyType = PlayerIdentityType.NORMAL_PLAYER;
			}
		}else if (identifyType == PlayerIdentityType.SUB_DISTRIBUTOR){
			subDistributorsModel =  DistributorInviteCodeDAO.getSubDistributorsModelByGuid(playerId);
			if(subDistributorsModel == null){
				identifyType = PlayerIdentityType.NORMAL_PLAYER;
			}else {
				distributorsModel = DistributorInviteCodeDAO.getDistributorModelByGUID(subDistributorsModel.getDistributorGuid());
				if(distributorsModel == null ||!distributorsModel.isEffective()){
					identifyType = PlayerIdentityType.NORMAL_PLAYER;
				}
			}
		}
		
		
		if(identifyType == PlayerIdentityType.NORMAL_PLAYER){
			response.setErrorCode(ErrorCode.DATA_NOT_FOUND);
			response.setPlayerType(identifyType);
			MessageAction.sendMessage(sessionId, messageType, response);
			return;
		}
		
		ArrayList<DistributorInviteCodesModel> inviteCodes = DistributorInviteCodeDAO.getDistributorInviteCodeModels(playerId, player.getPlayerIdentityType(),limitCount);
		
		ArrayList<InviteCodeData> playerInviteCodeDatas = new ArrayList<InviteCodeData>();
		for (DistributorInviteCodesModel inviteCodeModel : inviteCodes) {
			InviteCodeData data = new InviteCodeData();
			data.setInviteCode(inviteCodeModel.getInviteCode());
			data.setUsed(inviteCodeModel.isUsed());
			playerInviteCodeDatas.add(data);
		}
		
		int usedNum = DistributorInviteCodeDAO.getUsedDistributorInviteCodeNum(playerId, identifyType);
		
		int totalNum = (identifyType == PlayerIdentityType.DISTRIBUTOR || identifyType == PlayerIdentityType.BOTH_DISTRIBUTOR_DOWNLINE) ? distributorsModel.getInviteCodeCount() : subDistributorsModel.getInviteCodeNum();
		
		response.setInviteCodeDatas(playerInviteCodeDatas);
		response.setPlayerType(identifyType);
		response.setUsedNum(usedNum);
		response.setTotalNum(totalNum);
		response.setErrorCode(ErrorCode.SUCCESS);
		MessageAction.sendMessage(sessionId, messageType, response);
	}

	@Override
	public void execute() throws Exception {
		
	}

}
